/**
 * @fileoverview Giant Interective Group, Inc. Javascript Library v#version.
 * 该Javascript UI库是基于jQuery的扩展。
 * <pre>
 * Copyright (C) 2004-2009 Giant Interective Group, Inc. All rights reserved.
 * 版权所有 2004-2009 上海巨人网络科技有限公司
 * </pre>
 * @version #version, #date
 * @author  Zhangkai
 * Depend on jQuery 1.3.x
 */
if (typeof giant === 'undefined')
    var giant = window.giant = {};
if (typeof giant.ui === 'undefined')
    giant.ui = {};
(function($) {
    /**
     * 创建一个tagsuggest标签提示实例
     * @class giant.ui.tagsuggest 提示组件
     * @return {giant.ui.tagsuggest}的一个实例
     * @example
     * <pre>
     *  $(document).ready(function(){
     *   var tagSuggest = new giant.ui.tagsuggest({
     *       input:"#txtTag",
     *       description:"热门标签",
     *       tagArray:"['电影','音乐','大屏幕','影院','学习','美女','好莱坞']"
     *   });
     *  });
     * </pre>
     */
    giant.ui.tagsuggest = function(options) {
        this.opts = $.extend({}, giant.ui.tagsuggest.defaults, options);
        this.id = giant.ui.tagsuggest.PREFIX + giant.ui.tagsuggest.AUTOID;
        this.$tagsuggest = null;
        giant.ui.tagsuggest.AUTOID++;
        this.inputFocus = false;
        this.resultArr = [];
        this._init();
    };
    giant.ui.tagsuggest.PREFIX = "ui-tagsuggest-";
    giant.ui.tagsuggest.AUTOID = 1000;
    giant.ui.tagsuggest.prototype = {
        _init:function() {
            this._getTemp();
            this._setPosition();
            this._eventBind();
        },
        _getTemp:function() {
            var arr = [];
            arr.push('<div class="ui-tagsuggest" ');
            arr.push('id="' + this.id + '">');
            if (this.opts.description.length > 0) {
                arr.push('<div class="ui-tagsuggest-info llight1">');
                arr.push(this.opts.description);
                arr.push('</div>');
            }
            var arrTag = eval(this.opts.tagArray);
            if (arrTag.length > 0) {
                arr.push('<div class="ui-tagsuggest-taglist">');
                for (var i = 0; i < arrTag.length; i++) {
                    arr.push("<a href='javascript:void(1)'>" + arrTag[i] + "</a>");
                }
                arr.push("</div>");
            }
            arr.push("<div>");
            $("body").append(arr.join(''));
            this.$tagsuggest = $("#" + this.id);
        },
        _setPosition:function() {
            var $input = $(this.opts.input);
            this.$tagsuggest.css({"top":$input.offset().top + $input.outerHeight() - 1 + "px","left":$input.offset().left + "px","width":$input.outerWidth() - 12 + "px"});
        },
        _tagClickBind:function(){
          var $this = this;
          this.$tagsuggest.find(".ui-tagsuggest-taglist a").bind("click", function() {
                if ($.inArray($(this).text(), $this.resultArr) == -1) {
                    $this.resultArr.push($(this).text());
                    $($this.opts.input).val($this.resultArr.join(' '));
                }
            });  
        },
        _eventBind:function() {
            this._tagClickBind();
            var $this = this;
            $(this.opts.input).bind("focus", function() {
                $this.inputFocus = true;
                $this.show();
            });
            $(this.opts.input).bind("keyup", function() {
                  $this.resultArr = $.trim($(this).val()).split(' ');
            });
            $(document).bind("click", function(event) {
                if (!$this.inputFocus && $(event.target).parents(".ui-tagsuggest").size() == 0) {
                    $this.hide();
                }
            });
            $(this.opts.input).bind("blur", function() {
                $this.inputFocus = false;
            });
        },
        /*
        * */
        setTagList:function(arrTag) {
            arrTag = eval(arrTag);
            var arr = [];
            for (var i = 0; i < arrTag.length; i++) {
                arr.push("<a href='javascript:void(1)'>" + arrTag[i] + "</a>");
            }
            this.$tagsuggest.find(".ui-tagsuggest-taglist").html(arr.join(''));
            this._tagClickBind();
        },
        /**
         * 文本输入框清空
         */
        clearInput:function() {
            $(this.opts.input).val("");
        },

       /**
        * @return{Array} 添加的tag结果.
        */
        getResult:function(){
          return this.resultArr;  
        },
         /**
         * 显示提示
         */
        show:function() {
            this.$tagsuggest.show("fast");
        },
        /**
         * 隐藏提示
         */
        hide:function() {
            this.$tagsuggest.hide("fast");
        }
    };
    /**
     * @default 默认参数.
     * defaults.input:需要提示的文本框,如#txtTag
     * defaults.tagArray:需要显示的tag数组.可以是字符串,若为空则不显示热门标签,只显示描述.
     * description 提示上面的描述,若为空则不显示描述
     */
    giant.ui.tagsuggest.defaults = {
        input:"input",
        tagArray:'["标签加载中..."]',
        description:""
    };
})(jQuery);